package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Order;
import com.mju.pojo.OrderReq;
import com.mju.pojo.Receive;
import com.mju.service.IOrderService;
import com.mju.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orders")
@Api(tags = "订单信息服务")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;


    // 创建订单
    @ApiOperation("创建订单")
    @RequestMapping("/createOrder")
    public JsonResult<Order> createOrder(Integer aid, Integer[] cids, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");
        Order data = orderService.createOrder(aid, cids, Integer.valueOf(uid), uname);
        return new JsonResult<>(OK, data);
    }

    // 根据token创建订单
    @ApiOperation("根据token创建订单")
    @PostMapping("/createOrderByToken")
    public JsonResult<Order> createOrderByToken(
            @RequestParam("aid") Integer aid,@RequestParam("cids") Integer[] cids,@RequestParam("token") String token) {
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");
        Order data = orderService.createOrder(aid, cids, Integer.valueOf(uid), uname);
        return new JsonResult<>(OK, data);
    }

    // 创建订单项
    @ApiOperation("创建订单项")
    @PostMapping("/createOrderItem")
    public JsonResult<Void> createOrderItem(Integer oid,Integer cid,Integer num,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uname = map.get("uname");

        //调用业务层方法执行插入操作
        orderService.createOrderItem(oid,cid,num,uname);
        return new JsonResult<>(OK);
    }

    //处理根据oid查询订单的请求
    @ApiOperation("根据订单号查询所有子订单项")
    @GetMapping("/queryOrderVoByOid")
    public JsonResult<List<OrderVo>>  queryOrderVoByOid(@RequestParam("oid") Integer oid){
        List<OrderVo> orderVos = orderService.queryOrderVoByOid(oid);
        return new JsonResult<>(OK,orderVos);
    }

    @ApiOperation("根据用户id查询详情订单信息")
    @GetMapping("/queryOrderVoByUid")
    public JsonResult<List<OrderVo>>  queryOrderVoByUid(Integer uid){
        List<OrderVo> orderVos = orderService.queryOrderVoByUid(uid);
        return new JsonResult<>(OK,orderVos);
    }

    @ApiOperation("根据用户名查询订单详情信息")
    @GetMapping("/queryOrderVoByUname/{uname}")
    public JsonResult<List<OrderVo>>  queryOrderVoByUname(@PathVariable("uname") String uname){
        List<OrderVo> data = orderService.queryOrderVoByUname(uname);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据用户id查询订单")
    @GetMapping("/queryOrderByUid/{uid}")
    public JsonResult<List<Order>>  queryOrderByUid(@PathVariable("uid") Integer uid){
        List<Order> data = orderService.queryOrderByUid(uid);
        return new JsonResult<>(OK, data);
    }

    // 根据oid修改订单状态 状态：0-未支付，1-已支付，2-已取消，3-已关闭，4-已完成，5-已退款，6-退款异常
    @ApiOperation("根据订单号修改订单状态")
    @PutMapping("/updateOrderStatus/{oid}/{status}")
    public JsonResult<Void> updateOrderStatus(@PathVariable("oid")Integer oid,@PathVariable("status")Integer status) {
        Boolean data = orderService.updateStatusByOid(oid,status);
        return new JsonResult<>(OK, data+"");
    }

    //无条件查询所有订单信息
    @ApiOperation("无条件查询所有订单信息")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<Order>> queryAllOderInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<Order> data = orderService.queryAllOrderInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    //条件查询所有订单信息
    @ApiOperation("条件查询所有订单信息")
    @PostMapping("/conQueryAllOderInfo/{currentPage}/{pageSize}")
    public JsonResult<IPage<Order>> conQueryAllOrderInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize,@RequestBody Order order){
        IPage<Order> data = orderService.conQueryAllOrderInfo(currentPage, pageSize,order);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = orderService.conQueryAllOrderInfo((int)data.getPages(), pageSize,order);
        }
        return new JsonResult<>(OK,data);
    }

    // 查询所有订单项(根据订单号)
    @ApiOperation("根据订单号查询所有订单项")
    @GetMapping("/queryOrderItem/{oid}")
    public JsonResult<List<OrderVo>> queryOrderItemByOid(@PathVariable("oid") Integer oid) {
        List<OrderVo> data = orderService.queryOrderItemByOid(oid);
        return new JsonResult<>(OK, data);
    }

    // 删除订单
    @ApiOperation("删除订单")
    @DeleteMapping("/{oid}")
    public JsonResult<Integer> deleteOrder(@PathVariable Integer oid) {
        Integer data = orderService.deleteOrderByOid(oid);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据oid查询订单")
    @GetMapping("/queryOrderByOid/{oid}")
    public JsonResult<Order> queryOrderByOid(@PathVariable("oid") Integer oid) {
        Order order = orderService.queryOrderByOid(oid);
        return new JsonResult<>(OK, order);
    }

    @ApiOperation("查询用户的所有订单的所有订单项")
    @GetMapping("/queryAllOrderAndOrderItem")
    public JsonResult<List<List<OrderVo>>> queryAllOrderAndOrderItem(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<List<OrderVo>> data = orderService.queryAllOrderAndOrderItem(Integer.parseInt(uid));
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据状态，无条件查询订单信息")
    @GetMapping("/queryOrderInfoByStatus/{status}")
    public JsonResult<List<OrderVo>> queryOrderInfoByStatus(@PathVariable("status") Integer status) {
        List<OrderVo> orderVoList = orderService.queryOrderInfoByStatus(status);
        return new JsonResult<>(OK, orderVoList);
    }

    @ApiOperation("管理员发货")
    @PutMapping("/sendGoods")
    public JsonResult<Void> sendGoods(@RequestBody OrderReq orderReq) {
        Integer oid = orderReq.getOid();
        Integer expressId = orderReq.getExpressId();
        String expressName = orderReq.getExpressName();
        orderService.sendGoods(oid, expressId, expressName);
        return new JsonResult<>(OK, "发货成功");
    }
    @ApiOperation("用户发起退款")
    @PutMapping("/refund")
    public JsonResult<Void> refund(@RequestBody OrderReq orderReq) {
        orderService.refund(orderReq.getOid(), orderReq.getRefundReason());
        return new JsonResult<>(OK, "成功发起退款");
    }

    @ApiOperation("管理员审核退款")
    @GetMapping("/examineRefundInfo/{oid}")
    public JsonResult<Void> examineRefundInfo(@PathVariable("oid") Integer oid) {
        orderService.examineRefundInfo(oid);
        return new JsonResult<>(OK, "退款审核通过");
    }

    @ApiOperation("管理员拒绝退款")
    @PostMapping("/rejectRefund")
    public JsonResult<Void> rejectRefund(@RequestBody OrderReq orderReq) {
        orderService.rejectRefund(orderReq.getOid(), orderReq.getRejectReason());
        return new JsonResult<>(OK, "拒绝成功");
    }

    @ApiOperation("删除订单")
    @PostMapping("/deleteOrder")
    public JsonResult<Void> deleteOrder(@RequestBody OrderReq req) {
        orderService.deleteOrder(req.getOid());
        return new JsonResult<>(OK, "删除成功");
    }

    @ApiOperation("确认收货")
    @PutMapping("/finishOrder")
    public JsonResult<Void> finishOrder(@RequestBody OrderReq req) {
        orderService.finishOrder(req.getOid());
        return new JsonResult<>(OK, "确认收货成功");
    }

    @ApiOperation("订单评价")
    @PutMapping("/commentOrder")
    public JsonResult<Void> commentOrder(@RequestBody OrderReq req) {
        orderService.commentOrder(req.getOid(), req.getComment());
        return new JsonResult<>(OK, "评价成功");
    }

    @ApiOperation("根据状态获取订单信息")
    @GetMapping("/queryOrderByStatus/{status}")
    public JsonResult<List<List<OrderVo>>> queryOrderByStatus(HttpServletRequest request, @PathVariable("status") Integer status) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<List<OrderVo>> data = orderService.queryOrderByStatus(Integer.parseInt(uid), status);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据title过滤订单数据")
    @GetMapping("/queryOrderByTitle/{title}")
    public JsonResult<List<List<OrderVo>>> queryOrderByTitle(HttpServletRequest request, @PathVariable("title") String title) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<List<OrderVo>> data = orderService.queryOrderByTitle(Integer.parseInt(uid), title);
        return new JsonResult<>(OK, data);
    }


    //条件查询所有回收单信息
    @ApiOperation("条件查询所有order信息，条件最多包含回收单id、用户id、模糊用户名、回收单状态")
    @PostMapping("/conQueryAllReceiveInfo")
    public JsonResult<IPage<Receive>> conQueryAllReceiveInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,@RequestBody Order order){
        IPage<Receive> data = orderService.conQueryAllReceiveInfo(currentPage, pageSize,order);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = orderService.conQueryAllReceiveInfo((int)data.getPages(), pageSize,order);
        }
        return new JsonResult<>(OK,data);
    }
}
